//
//  ViewController.m
//  Reader
//
//  Created by mac on 16/12/6.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ViewController.h"
#import "MYCollectionViewCell.h"

@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
{
    NSMutableArray <UIImage *> *images;
    NSMutableArray <NSString *> *imageName;
}
@property (strong, nonatomic) UICollectionView *collectionView;
@property (strong, nonatomic) UICollectionViewFlowLayout *layout;
@end

@implementation ViewController

static NSString * const identifier = @"cell";

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadImageData];
    
    self.layout = [[UICollectionViewFlowLayout alloc] init];
    self.collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:self.layout];
    [self.collectionView registerClass:[MYCollectionViewCell class] forCellWithReuseIdentifier:identifier];

    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    self.layout.minimumLineSpacing = 20;
    self.layout.minimumInteritemSpacing = 15;
    self.layout.itemSize = CGSizeMake((self.view.frame.size.width - 30) / 3, (self.view.frame.size.height - 64 - 40) / 3);
    
    [self.view addSubview:self.collectionView];
}

- (void)loadImageData
{
    if(!images)
    {
        images = [NSMutableArray new];
    }
    if(!imageName)
    {
        imageName = [NSMutableArray new];
    }
    
    
    NSArray <NSString *> *arr = [[NSBundle mainBundle] pathsForResourcesOfType:@".jpeg" inDirectory:@"资源/imag"];
    for(NSString *str in arr)
    {
        NSString *name = [str componentsSeparatedByString:@"/"].lastObject;
        [imageName addObject:name];
    }
    NSLog(@"ImageName:%@",imageName);
    
    
    for(NSString *name in imageName)
    {
        UIImage *image = [UIImage imageNamed:[@"资源/imag" ]];
        [images addObject:image];
    }
    NSLog(@"数量：%ld",images.count);
}


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return images.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MYCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    cell.imgView.image = images[indexPath.row];
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
